
rm(list=ls())

#setwd("/Users/christopherblair/Dropbox/MGAR Cleanup October 2017/Replication Files/")

library(survival)
library(coxme)
library(frailtySurv)
library(frailtyEM)
library(frailtypack)
library(nmisc)
library(xfun)
library(Hmisc)
library(simPH)
library(haven)
library(foreign)
library(stargazer)
library(coefplot)
library(xtable)
library(texreg)
library(list)
library(haven)

set.seed(8675309)

data <- read_dta("mgarduration_undirected.dta")

samestate <- subset(data, samestatedyad== 1)
region <- subset(data, samecontig== 1)
transnational <- subset(data, transnational2== 1)
coldwar <- subset(data, coldwar== 1)
post911 <- subset(data, post911== 1)
isisaq <- subset(data, isis== 0 & alqaeda==0)

## set stricter convergence criteria
coxph.control(eps = sqrt(.Machine$double.eps), toler.chol = .Machine$double.eps,
              iter.max = 1000, toler.inf = sqrt(.Machine$double.eps), outer.max = 1000)

coxme.control(eps = sqrt(.Machine$double.eps), toler.chol = .Machine$double.eps,
              iter.max = 1000)

## Material Alliance Terminations

m1 <- coxph(formula = Surv(matstart, matend, term) ~
               sharedideo2+fariss+
               polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
               ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
               ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
               
               cluster(undir_id)+
               strata(nummat),
             data = data,
             method = c("efron"),
             model=T)

summary(m1)

m1a <- coxph(formula = Surv(matstart, matend, term) ~
               sharedideo2*fariss+
               polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
               ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
               ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
               
               cluster(undir_id)+
               strata(nummat),
             data = data,
             method = c("efron"),
             model=T)

summary(m1a)

m1b <- coxph(formula = Surv(matstart, matend, term) ~
               nosharedideo*fariss+
               polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
               ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
               ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
               
               cluster(undir_id)+
               strata(nummat),
             data = data,
             method = c("efron"),
             model=T)

summary(m1b)

# Marginal Effects

SimInt1 <- coxsimInteract(m1a, b1 = "sharedideo2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

matalliance_points<-simGG(SimInt1, type = "points", alpha = .4, xlab = "\n Repression",
                   ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

matalliance_points<- matalliance_points + scale_x_continuous(breaks = seq(0, 3.1, by = .25)) + scale_y_continuous(breaks = seq(0, 4, by = .5))

matalliance_points<-matalliance_points + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

matalliance_points

ggsave('/Users/christopherblair/Desktop/matalliance_points.png', matalliance_points, width = 7, height = 4, units = 'in', dpi = 300)

SimInt1b <- coxsimInteract(m1b, b1 = "nosharedideo", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

matnoshared_points<-simGG(SimInt1b, type = "points", alpha = .4, xlab = "\n Repression",
                          ylab = "Marginal Effect of \n No Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

matnoshared_points<- matnoshared_points + scale_x_continuous(breaks = seq(0, 3.1, by = .25)) + scale_y_continuous(breaks = seq(0, 2.5, by = .25))

matnoshared_points<-matnoshared_points + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

matnoshared_points

ggsave('/Users/christopherblair/Desktop/matalliance_points.png', matalliance_points, width = 7, height = 4, units = 'in', dpi = 300)


## Rhetorical Alliance Terminations

m2 <- coxph(formula = Surv(rhetstart, rhetend, term) ~
               sharedideo2+fariss+
               polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
               ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
               ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
              
               cluster(undir_id),
             data = data,
             method = c("efron"))

summary(m2)

m2a <- coxph(formula = Surv(rhetstart, rhetend, term) ~
              sharedideo2*fariss+
              polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
              ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
              ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
              sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
              
                cluster(undir_id),
             data = data,
             method = c("efron"))

summary(m2a)

m2b <- coxph(formula = Surv(rhetstart, rhetend, term) ~
               nosharedideo*fariss+
               polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
               ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
               ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
               
               cluster(undir_id),
             data = data,
             method = c("efron"))

summary(m2b)

texreg(list(m2, m2a, m1, m1a), omit.coef="gamma", digits = 3, stars = c(0.01, 0.05, 0.1), include.zph = F)

# Marginal Effects

SimInt2 <- coxsimInteract(m2a, b1 = "sharedideo2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=T, extremesDrop=T)

rhetalliance_points<-simGG(SimInt2, type = "points", alpha = 1, xlab = "\n Repression",
                    ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rhetalliance_points<- rhetalliance_points + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 30, by = 5))

rhetalliance_points<-rhetalliance_points + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rhetalliance_points

ggsave('rhetalliance_points.png', rhetalliance_points, width = 7, height = 4, units = 'in', dpi = 300)

SimInt2b <- coxsimInteract(m2b, b1 = "nosharedideo", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=T, extremesDrop=T)

rhetalliance_points<-simGG(SimInt2, type = "points", alpha = 1, xlab = "\n Repression",
                           ylab = "Marginal Effect of \n No Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rhetalliance_points<- rhetalliance_points + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 40, by = 5))

rhetalliance_points<-rhetalliance_points + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rhetalliance_points

ggsave('rhetallianceno_points.png', rhetalliance_points, width = 7, height = 4, units = 'in', dpi = 300)


################################ POST 9/11 #############################

postmat1 <- coxph(formula = Surv(matstart, matend, term) ~
                 corelgdyad2*fariss*post911+
                 polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                 ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                 ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                 sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                 
                 cluster(undir_id)+
                 strata(nummat),
               data = data,
               method = c("efron"),
               model=T)

summary(postmat1)

postmat2 <- coxph(formula = Surv(matstart, matend, term) ~
                    corelgdyad2*fariss+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    capratio_undir +
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                    
                    cluster(undir_id)+
                    strata(nummat),
                  data = post911,
                  method = c("efron"),
                  model=T)

summary(postmat2)

postmat4 <- coxph(formula = Surv(matstart, matend, term) ~
                    nocorelg*fariss+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    capratio_undir +
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                    
                    cluster(undir_id)+
                    strata(nummat),
                  data = post911,
                  method = c("efron"),
                  model=T)

summary(postmat4)

SimPost2 <- coxsimInteract(postmat2, b1 = "corelgdyad2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

postmat_points2<-simGG(SimPost2, type = "points", alpha = .4, xlab = "\n Repression",
                          ylab = "Marginal Effect of \n Shared Religion \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

postmat_points2<- postmat_points2 + scale_x_continuous(breaks = seq(0, 3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 4, by = .5))

postmat_points2<-postmat_points2 + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

postmat_points2

SimPost4 <- coxsimInteract(postmat4, b1 = "nocorelg", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=T, extremesDrop=T)

postmat_points4<-simGG(SimPost4, type = "points", alpha = .4, xlab = "\n Repression",
                       ylab = "Marginal Effect of \n No Shared Religion \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

postmat_points4<- postmat_points4 + scale_x_continuous(breaks = seq(0, 3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 5, by = .5))

postmat_points4<-postmat_points4 + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

postmat_points4

postrhet1 <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                  corelgdyad2*fariss*post911+
                  polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                  ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                  ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                  sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
                  
                  cluster(undir_id),
                data = data,
                method = c("efron"))

summary(postrhet1)

postrhet2 <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                     corelgdyad2*fariss+
                     polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                     capratio_undir +
                     sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
                     
                     cluster(undir_id),
                   data = post911,
                   method = c("efron"))

summary(postrhet2)

SimPost5 <- coxsimInteract(postrhet2, b1 = "corelgdyad2", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(0, 2.5, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=T, extremesDrop=T)

postmat_points5<-simGG(SimPost5, type = "points", alpha = .4, xlab = "\n Repression",
                       ylab = "Marginal Effect of \n Shared Religion \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

postmat_points5<- postmat_points5 + scale_x_continuous(breaks = seq(0, 2.5, by=.25)) + scale_y_continuous(breaks = seq(0, 12, by = 1))

postmat_points5<-postmat_points5 + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

postmat_points5

texreg(list(postrhet2, postmat2), omit.coef="gamma", digits = 3, stars = c(0.01, 0.05, 0.1), include.zph = F)


################################ COLD WAR #############################

cwmat1 <- coxph(formula = Surv(matstart, matend, term) ~
                 leftcommdyad*fariss*coldwar+
                 polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                 ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                 ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                 sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                 
                 cluster(undir_id)+
                 strata(nummat),
               data = data,
               method = c("efron"),
               model=T)

summary(cwmat1)

cwmat2 <- coxph(formula = Surv(matstart, matend, term) ~
                  leftcommdyad*fariss+
                  polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                  capratio_undir +
                  sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                  
                  cluster(undir_id)+
                  strata(nummat),
                data = coldwar,
                method = c("efron"),
                model=T)

summary(cwmat2)

cwmatno <- coxph(formula = Surv(matstart, matend, term) ~
                  noleftcomm*fariss+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                   capratio_undir +
                   sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                  
                  cluster(undir_id)+
                  strata(nummat),
                data = coldwar,
                method = c("efron"),
                model=T)

summary(cwmatno)

SimPostCW <- coxsimInteract(cwmat2, b1 = "leftcommdyad", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(0,3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

cwmat_points3<-simGG(SimPostCW, type = "points", alpha = .4, xlab = "\n Repression",
                       ylab = "Marginal Effect of \n Shared Leftism \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

cwmat_points3<- cwmat_points3 + scale_x_continuous(breaks = seq(0,3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 3, by = .25))

cwmat_points3<-cwmat_points3 + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

cwmat_points3

SimPostCWNo <- coxsimInteract(cwmatno, b1 = "noleftcomm", b2 = "fariss",
                            qi = "Marginal Effect",
                            X2 = seq(0,3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

cwmat_points4<-simGG(SimPostCWNo, type = "points", alpha = .4, xlab = "\n Repression",
                     ylab = "Marginal Effect of \n No Shared Leftism \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

cwmat_points4<- cwmat_points4 + scale_x_continuous(breaks = seq(0,3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 3, by = .25))

cwmat_points4<-cwmat_points4 + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

cwmat_points4

cwrhet <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                  leftcommdyad*fariss*coldwar+
                  polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                  ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                  ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                  sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
                  
                  cluster(undir_id),
                data = data,
                method = c("efron"))

summary(cwrhet)

cwrhet2 <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                  leftcommdyad*fariss+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                  capratio_undir +
                  sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
                  
                  cluster(undir_id),
                data = coldwar,
                method = c("efron"))

summary(cwrhet2)

SimPostCWRhet <- coxsimInteract(cwrhet2, b1 = "leftcommdyad", b2 = "fariss",
                            qi = "Marginal Effect",
                            X2 = seq(0,3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

cwrhet<-simGG(SimPostCWRhet, type = "points", alpha = .4, xlab = "\n Repression",
                     ylab = "Marginal Effect of \n Shared Leftism \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

cwrhet<- cwrhet + scale_x_continuous(breaks = seq(0,3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 5, by = .5))

cwrhet<-cwrhet + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

cwrhet

texreg(list(cwrhet2, cwmat2), omit.coef="gamma", digits = 3, stars = c(0.01, 0.05, 0.1), include.zph = F)

################################ OTHER IDEOLOGIES #############################

matnat <- coxph(formula = Surv(matstart, matend, term) ~
                  natsepdyad*fariss+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                   ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                   ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                   sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                   
                   cluster(undir_id)+
                   strata(nummat),
                 data = data,
                 method = c("efron"),
                 model=T)

summary(matnat)

rhetnat <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                    natsepdyad*fariss+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ethfrac1 +ethfrac2 +
                    ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                    ln_ustroops1_lag1+ ln_ustroops2_lag1+
                   capratio_undir +
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+numrhet+
                    
                    cluster(undir_id),
                  data = data,
                  method = c("efron"),
                 model=T)

summary(rhetnat)

texreg(list(rhetnat, matnat), omit.coef="gamma", digits = 3, stars = c(0.01, 0.05, 0.1), include.zph = F)

SimPostNatRhet <- coxsimInteract(rhetnat, b1 = "natsepdyad", b2 = "fariss",
                                qi = "Marginal Effect",
                                X2 = seq(0,3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

natrhet<-simGG(SimPostNatRhet, type = "points", alpha = .4, xlab = "\n Repression",
              ylab = "Marginal Effect of \n Shared Nationalism/Separatism \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

natrhet<- natrhet + scale_x_continuous(breaks = seq(0,3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 5, by = .5))

natrhet<-natrhet + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

natrhet

SimPostNatMat <- coxsimInteract(matnat, b1 = "natsepdyad", b2 = "fariss",
                                 qi = "Marginal Effect",
                                 X2 = seq(0,3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

natmat<-simGG(SimPostNatMat, type = "points", alpha = .4, xlab = "\n Repression",
               ylab = "Marginal Effect of \n Shared Nationalism/Separatism \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

natmat<- natmat + scale_x_continuous(breaks = seq(0,3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 5, by = .5))

natmat<-natmat + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

natmat

## Shared Right-Wing

matright <- coxph(formula = Surv(matstart, matend, term) ~
                  rightdyad*fariss+
                  polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                  ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                  ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                  sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                  
                  cluster(undir_id)+
                  strata(nummat),
                data = data,
                method = c("efron"),
                model=T)

summary(matright)

rhetright <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                     rightdyad*fariss+
                     polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    capratio_undir +
                     sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                   
                   cluster(undir_id),
                 data = data,
                 method = c("efron"), model=T)

summary(rhetright)

texreg(list(rhetright, matright), omit.coef="gamma", digits = 3, stars = c(0.01, 0.05, 0.1), include.zph = F)

SimPostRightRhet <- coxsimInteract(rhetright, b1 = "rightdyad", b2 = "fariss",
                                 qi = "Marginal Effect",
                                 X2 = seq(0,2.5, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

rightrhet<-simGG(SimPostRightRhet, type = "points", alpha = .4, xlab = "\n Repression",
               ylab = "Marginal Effect of \n Shared Right-Wing \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rightrhet<- rightrhet + scale_x_continuous(breaks = seq(0,3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 5, by = .5))

rightrhet<-rightrhet + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rightrhet

SimPostRightMat <- coxsimInteract(matright, b1 = "rightdyad", b2 = "fariss",
                                qi = "Marginal Effect",
                                X2 = seq(0,3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

rightmat<-simGG(SimPostRightMat, type = "points", alpha = .4, xlab = "\n Repression",
              ylab = "Marginal Effect of \n Shared Right-Wing \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rightmat<- rightmat + scale_x_continuous(breaks = seq(0,3.1, by=.25)) + scale_y_continuous(breaks = seq(0, 7, by = .5))

rightmat<-rightmat + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rightmat

######################## ALT DV -- TYPE #########

mattype <- coxph(formula = Surv(matstart_type, matend_type, term) ~
                   sharedideo2*fariss+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                   ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                   ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                   sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                   
                   cluster(undir_id)+
                   strata(nummat_type),
                 data = data,
                 method = c("efron"),
                 model=T)

summary(mattype)

TypeMat <- coxsimInteract(mattype, b1 = "sharedideo2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=F)

typemat <-simGG(TypeMat, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

typemat<- typemat + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 7, by = .5))

typemat<-typemat + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

typemat

ggsave('typemat.png', rep2mat, width = 7, height = 4, units = 'in', dpi = 300)

rhettype <- coxph(formula = Surv(rhetstart_type, rhetend_type, term) ~
                    sharedideo2*fariss+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                    ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet_type)+
                    
                    cluster(undir_id),
                  data = data,
                  method = c("efron"))

summary(rhettype)

TypeRhet <- coxsimInteract(rhettype, b1 = "sharedideo2", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(1, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=T, extremesDrop=T)

typerhet<-simGG(TypeRhet, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

typerhet<- typerhet + scale_x_continuous(breaks = seq(1, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 40, by = 5))

typerhet<-typerhet + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

typerhet

ggsave('typerhet.png', rhetrep2, width = 7, height = 4, units = 'in', dpi = 300)


######################## ALT REPRESSION MEASURE #########

matrep2 <- coxph(formula = Surv(matstart, matend, term) ~
                   sharedideo2*repression+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                   ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                   ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                   sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                   
                   cluster(undir_id)+
                   strata(nummat),
                 data = data,
                 method = c("efron"),
                 model=T)

summary(matrep2)

Rep2Mat <- coxsimInteract(matrep2, b1 = "sharedideo2", b2 = "repression",
                          qi = "Marginal Effect",
                          X2 = seq(0, 8, by=1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=F)

rep2mat <-simGG(Rep2Mat, type = "points", alpha = 1, xlab = "\n CIRI Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rep2mat<- rep2mat + scale_x_continuous(breaks = seq(0, 8, by = 1))+scale_y_continuous(breaks = seq(0, 7, by = .5))

rep2mat<-rep2mat + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rep2mat

ggsave('rep2mat.png', rep2mat, width = 7, height = 4, units = 'in', dpi = 300)

rhetrep2 <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                    sharedideo2*repression+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                    ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+numrhet+
                    
                    cluster(undir_id),
                  data = data,
                  method = c("efron"))

summary(rhetrep2)

Rep2Rhet <- coxsimInteract(rhetrep2, b1 = "sharedideo2", b2 = "repression",
                           qi = "Marginal Effect",
                           X2 = seq(0, 8, by=1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

rhetrep2<-simGG(Rep2Rhet, type = "points", alpha = 1, xlab = "\n CIRI Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rhetrep2<- rhetrep2 + scale_x_continuous(breaks = seq(0, 8, by = 1))+scale_y_continuous(breaks = seq(0, 30, by = 5))

rhetrep2<-rhetrep2 + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rhetrep2

ggsave('rep2rhet.png', rhetrep2, width = 7, height = 4, units = 'in', dpi = 300)


######################## IPTW #########

matiptw <- coxph(formula = Surv(matstart, matend, term) ~
                   sharedideo2*fariss+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                   ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                   ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                   sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                   
                   cluster(undir_id)+
                   strata(nummat),
                 data = data,
                 method = c("efron"),
                 weights=weights,
                 model=T)

summary(matiptw)

IPTWMat <- coxsimInteract(matiptw, b1 = "sharedideo2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=F)

iptwmat <-simGG(IPTWMat, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

iptwmat<- iptwmat + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 7, by = .5))

iptwmat<-iptwmat + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

iptwmat

ggsave('iptwmat.png', iptwmat, width = 7, height = 4, units = 'in', dpi = 300)

rhetiptw <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                    sharedideo2*fariss+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                    ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
                    
                    cluster(undir_id),
                  data = data,
                  method = c("efron"),
                  weights=weights)

summary(rhetiptw)

IPTWRhet <- coxsimInteract(rhetiptw, b1 = "sharedideo2", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

iptwrhet<-simGG(IPTWRhet, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

iptwrhet<- iptwrhet + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 30, by = 3))

iptwrhet<-iptwrhet + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

iptwrhet

ggsave('iptwrhet.png', iptwrhet, width = 7, height = 4, units = 'in', dpi = 300)


######################## 2SRI #########

mat2sri <- coxph(formula = Surv(matstart, matend, term) ~
                   sharedideo2*fariss+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                   ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                   ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                   sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+ residual_repression+
                   
                   cluster(undir_id)+
                   strata(nummat),
                 data = data,
                 method = c("efron"),
                 model=T)

summary(mat2sri)

MAT2SRI <- coxsimInteract(mat2sri, b1 = "sharedideo2", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

twosrimat<-simGG(MAT2SRI, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

twosrimat<- twosrimat + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 4, by = .4))

twosrimat<-twosrimat + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

twosrimat

ggsave('twosrimat.png', twosrimat, width = 7, height = 4, units = 'in', dpi = 300)


rhet2sri <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                    sharedideo2*fariss+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                    ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir+
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+residual_repression+
                    
                    cluster(undir_id),
                  data = data,
                  method = c("efron"))

summary(rhet2sri)

RHET2SRI <- coxsimInteract(rhet2sri, b1 = "sharedideo2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

twosrirhet<-simGG(RHET2SRI, type = "points", alpha = 1, xlab = "\n Repression",
                 ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

twosrirhet<- twosrirhet + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 80, by = 10))

twosrirhet<-twosrirhet + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

twosrirhet

ggsave('twosrirhet.png', twosrirhet, width = 7, height = 4, units = 'in', dpi = 300)


################################ No ISIS or AQ #########

isisaqmat <- coxph(formula = Surv(matstart, matend, term) ~
                 sharedideo2*fariss+
                 polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                 ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                 ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                 sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                 
                 cluster(undir_id)+
                 strata(nummat),
               data = isisaq,
               method = c("efron"),
               model=T)

summary(isisaqmat)

ISISAQMat <- coxsimInteract(isisaqmat, b1 = "sharedideo2", b2 = "fariss",
                             qi = "Marginal Effect",
                             X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=F)

matisisaq<-simGG(ISISAQMat, type = "points", alpha = 1, xlab = "\n Repression",
                  ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

matisisaq<- matisisaq + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 7, by = .5))

matisisaq<-matisisaq + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

matisisaq

ggsave('matisisaq.png', matisisaq, width = 7, height = 4, units = 'in', dpi = 300)

isisaqrhet <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                      sharedideo2*fariss+
                  polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                  ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                  ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
                  sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+numrhet+
                  
                  cluster(undir_id),
                data = isisaq,
                method = c("efron"), model=T)

summary(isisaqrhet)

ISISAQRhet <- coxsimInteract(isisaqrhet, b1 = "sharedideo2", b2 = "fariss",
                            qi = "Marginal Effect",
                            X2 = seq(2, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=F)

rhetisisaq<-simGG(ISISAQRhet, type = "points", alpha = 1, xlab = "\n Repression",
                 ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rhetisisaq<- rhetisisaq + scale_x_continuous(breaks = seq(2, 3.1, by = .1))+scale_y_continuous(breaks = seq(0, 7, by = .5))

rhetisisaq<-rhetisisaq + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rhetisisaq

ggsave('rhetisisaq.png', rhetisisaq, width = 7, height = 4, units = 'in', dpi = 300)


######################## MISSING GTD DATA #########

matgtd <- coxph(formula = Surv(matstart, matend, term) ~
                   sharedideo2*fariss+
                   polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                   ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                   ln_ustroops1_lag1+ ln_ustroops2_lag1+capratiomiss_undir +
                   sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
                   
                   cluster(undir_id)+
                   strata(nummat),
                 data = data,
                 method = c("efron"),
                 model=T)

summary(matgtd)

GTDmat <- coxsimInteract(matgtd, b1 = "sharedideo2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=F)

gtdmat <-simGG(GTDmat, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

gtdmat<- gtdmat + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 7, by = .5))

gtdmat<-gtdmat + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

gtdmat

ggsave('gtdmat.png', gtdmat, width = 7, height = 4, units = 'in', dpi = 300)

rhetgtd <- coxph(formula = Surv(rhetstart, rhetend, term) ~
                    sharedideo2*fariss+
                    polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
                    ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
                    ln_ustroops1_lag1+ ln_ustroops2_lag1+capratiomiss_undir +
                    sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+numrhet+
                    
                    cluster(undir_id),
                  data = data,
                  method = c("efron"), model=T)

summary(rhetgtd)

GTDRhet <- coxsimInteract(rhetgtd, b1 = "sharedideo2", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(1.9, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

gtdrhet<-simGG(GTDRhet, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

gtdrhet<- gtdrhet + scale_x_continuous(breaks = seq(1.9, 3.1, by = .1))+scale_y_continuous(breaks = seq(0, 2, by = .2))

gtdrhet<-gtdrhet + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

gtdrhet

ggsave('gtdrhet.png', gtdrhet, width = 7, height = 4, units = 'in', dpi = 300)


############ Material Alliance Terminations Disaggregated by Area ###########

regionmat <- coxph(formula = Surv(matstart, matend, term) ~
               sharedideo2*fariss+
               polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
               ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
               ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
               
               cluster(undir_id)+
               strata(nummat),
             data = region,
             method = c("efron"),
             model=T)

summary(regionmat)

RegionMat <- coxsimInteract(regionmat, b1 = "sharedideo2", b2 = "fariss",
                          qi = "Marginal Effect",
                          X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

matregion<-simGG(RegionMat, type = "points", alpha = 1, xlab = "\n Repression",
               ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

matregion<- matregion + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 8, by = .5))

matregion<-matregion + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

matregion

ggsave('matregion.png', matregion, width = 7, height = 4, units = 'in', dpi = 300)

transmat <- coxph(formula = Surv(matstart, matend, term) ~
               sharedideo2*fariss+
               ethfrac1 +ethfrac2 +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+
               
               cluster(undir_id)+
               strata(nummat),
             data = transnational,
             method = c("efron"),
             model=T)

summary(transmat)

TransMat <- coxsimInteract(transmat, b1 = "sharedideo2", b2 = "fariss",
                            qi = "Marginal Effect",
                            X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

mattrans<-simGG(TransMat, type = "points", alpha = 1, xlab = "\n Repression",
                 ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

mattrans<- mattrans + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 8, by = .5))

mattrans<-mattrans + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Material Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

mattrans

ggsave('mattrans.png', mattrans, width = 7, height = 4, units = 'in', dpi = 300)


## Rhetorical Alliance Terminations Disaggregated by Area

regionrhet <- coxph(formula = Surv(rhetstart, rhetend, term) ~
               sharedideo2*fariss+
               polity1_lag1+ polity2_lag1+ politysquared1_lag1+ politysquared2_lag1+ ethfrac1 +ethfrac2 +
               ln_pop1_lag1 +ln_pop2_lag1 +ln_gdppc1_lag1 +ln_gdppc2_lag1 +
               ln_ustroops1_lag1+ ln_ustroops2_lag1+capratio_undir +
               sharedsponsor_lag1 +agediff +age1 +age2+ ln_dist+tt(numrhet)+
               
               cluster(undir_id),
             data = region,
             method = c("efron"))

summary(regionrhet)

RegionRhet <- coxsimInteract(regionrhet, b1 = "sharedideo2", b2 = "fariss",
                           qi = "Marginal Effect",
                           X2 = seq(0, 3.1, by=.1),expMarg = TRUE, nsim = 1000, ci = 0.90, spin=F, extremesDrop=T)

rhetregion<-simGG(RegionRhet, type = "points", alpha = 1, xlab = "\n Repression",
                ylab = "Marginal Effect of \n Shared Ideology \n", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess")

rhetregion<- rhetregion + scale_x_continuous(breaks = seq(0, 3.1, by = .25))+scale_y_continuous(breaks = seq(0, 26, by = 2))

rhetregion<-rhetregion + geom_hline(yintercept=1, linetype="dashed", color = "black") + 
  ggtitle("Probability of Rhetorical Alliance Breakdown")+
  theme(plot.title = element_text(hjust = 0.5))

rhetregion

ggsave('rhetregion.png', rhetregion, width = 7, height = 4, units = 'in', dpi = 300)


